package com.shuoyue.ycgk.ui.recruitment.newsinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.ui.recruitment.job.JobAdapter;
import com.shuoyue.ycgk.ui.recruitment.job.JobContract;
import com.shuoyue.ycgk.ui.recruitment.job.JobDetailActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentNoticeInfoJobs extends BaseMvpFragment<JobContract.Presenter>
        implements JobContract.View {

    int noticeId;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.info)
    TextView info;
    @BindView(R.id.jobRecycleView)
    RecyclerView jobRecycleView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshlayout;


    JobInRecurimentAdapter jobAdapter;
    String searchTitle;
    ListWithPage listWithPage;

    public static FragmentNoticeInfoJobs getInstance(int noticeId) {
        FragmentNoticeInfoJobs fragment = new FragmentNoticeInfoJobs();
        Bundle bundle = new Bundle();
        bundle.putSerializable("noticeId", noticeId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {
        noticeId = bundle.getInt("noticeId");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_info_joblist;
    }

    @Override
    protected void initView() {
        jobRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        jobRecycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));

        jobAdapter = new JobInRecurimentAdapter(null);
        jobRecycleView.setAdapter(jobAdapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshlayout -> {
            mPresenter.getJobs(null, noticeId, 1, searchTitle);
            refreshlayout.finishRefresh(1000);
        });
        refreshlayout.setOnLoadMoreListener(refreshlayout -> {
            mPresenter.getJobs(null, noticeId, listWithPage.getCurrent() + 1, searchTitle);
            refreshlayout.finishLoadMore(1000);
        });
        registEmptyView(jobAdapter);
        edit.setOnEditorActionListener((v, actionId, event) -> {
            String s = edit.getText().toString();
            if (!TextUtils.isEmpty(s)) {
                searchTitle = s;
                mPresenter.getJobs(null, noticeId, 1, searchTitle);
            }
            return true;
        });
        jobAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            JobInfo jobInfo = (JobInfo) adapter.getItem(position);
            startActivity(new Intent(mContext, JobDetailActivity.class)
                    .putExtra("id", jobInfo.getId()));
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new JobContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getJobs(null, noticeId, 1, null);
    }

    @Override
    public String getTitle() {
        return "职位列表";
    }


    @Override
    public void setJobList(ListWithPage<JobInfo> list) {
        listWithPage = list;
        info.setText("共有" + list.getTotal() + "个职位");
        if (list.getCurrent() == 1) {
            jobAdapter.resetData(list.getRecords());
        } else {
            jobAdapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages()&& list.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @OnClick(R.id.close)
    public void onViewClicked() {
        edit.setText("");
        mPresenter.getJobs(null, noticeId, 1, null);

    }
}
